﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPC.Properties;
using SCPutils;

namespace SPC.Classes
{
    class Penalty : BasicObject
    {
        private static string PENALTY_ID = "id";
        private static string PENALTY_PERSON_ID = "person_id";
        private static string PENALTY_VERSION = "version";
        private static string PENALTY_ATTRIB_DATE = "date";
        private static string PENALTY_ATTRIB_RATE = "rate";
        private static string PENALTY_ATTRIB_DUE_DATE = "due_date";
        private static string PENALTY_ATTRIB_PERCENT = "percent";
        private static string PENALTY_ATTRIB_DAYS_COUNT = "days_count";
        private static string PENALTY_ATTRIB_PENALTY_PER_DAY = "penalty_per_day";
        private static string PENALTY_ATTRIB_PENALTY_VALUE = "penalty_value";
        private static string PENALTY_ATTRIB_TOTAL_PENALTY = "total_penalty";


        public Penalty() { }

        public string getID()
        {
            return this.dictData[PENALTY_ID];
        }
        public void setID(string strValue)
        {
            this.dictData[PENALTY_ID] = Utils.Data._sql(strValue);
        }

        public string getPersonID()
        {
            return this.dictData[PENALTY_PERSON_ID];
        }
        public void setPersonID(string strValue)
        {
            this.dictData[PENALTY_PERSON_ID] = Utils.Data._sql(strValue);
        }

        public string getVersion()
        {
            return this.dictData[PENALTY_VERSION];
        }
        public string getMonthYearVersion()
        {
            long timestamp = long.Parse(this.dictData[PENALTY_VERSION]);
            DateTime versionDT = Utils.Unix.DateTimeFromUnixTimestampSeconds(timestamp);
            return versionDT.ToString("MM/yyyy");
        }
        public void setVersion(string strValue)
        {
            this.dictData[PENALTY_VERSION] = Utils.Data._sql(strValue);
        }

        public string getDate()
        {
            return this.dictData[PENALTY_ATTRIB_DATE];
        }
        public string getDate_MonthYearFormat()
        {
            return (Utils.Unix.DateTimeFromUnixTimestampSeconds (long.Parse(this.dictData[PENALTY_ATTRIB_DATE]))).ToString("MM.yyyy");
        }
        public void setDate(string strValue)
        {
            this.dictData[PENALTY_ATTRIB_DATE] = Utils.Data._sql(strValue);
        }
        public void setDate_FromMonthYear(string strValue)
        {
            this.dictData[PENALTY_ATTRIB_DATE] = Utils.Unix.GetUnixTimestampSecondsFromMonthYear(Utils.Data._sql(strValue)).ToString();
        }

        public string getRate()
        {
            return this.dictData[PENALTY_ATTRIB_RATE];
        }
        public void setRate(string strValue)
        {
            this.dictData[PENALTY_ATTRIB_RATE] = Utils.Data._sql(strValue);
        }

        public string getDueDate()
        {
            return this.dictData[PENALTY_ATTRIB_DUE_DATE];
        }
        public void setDueDate(string strValue)
        {
            this.dictData[PENALTY_ATTRIB_DUE_DATE] = Utils.Data._sql(strValue);
        }


        public string getPercent()
        {
            return this.dictData[PENALTY_ATTRIB_PERCENT];
        }
        public void setPercent(string strValue)
        {
            this.dictData[PENALTY_ATTRIB_PERCENT] = Utils.Data._sql(strValue);
        }


        public string getDaysCount()
        {
            return this.dictData[PENALTY_ATTRIB_DAYS_COUNT];
        }
        public void setDaysCount(string strValue)
        {
            this.dictData[PENALTY_ATTRIB_DAYS_COUNT] = Utils.Data._sql(strValue);
        }


        public string getPenaltyPerDay()
        {
            return this.dictData[PENALTY_ATTRIB_PENALTY_PER_DAY];
        }
        public void setPenaltyPerDay(string strValue)
        {
            this.dictData[PENALTY_ATTRIB_PENALTY_PER_DAY] = Utils.Data._sql(strValue);
        }


        public string getPenaltyValue()
        {
            return this.dictData[PENALTY_ATTRIB_PENALTY_VALUE];
        }
        public void setPenaltyValue(string strValue)
        {
            this.dictData[PENALTY_ATTRIB_PENALTY_VALUE] = Utils.Data._sql(strValue);
        }


        public string getTotalPenalty()
        {
            return this.dictData[PENALTY_ATTRIB_TOTAL_PENALTY];
        }
        public void setTotalPenalty(string strValue)
        {
            this.dictData[PENALTY_ATTRIB_TOTAL_PENALTY] = Utils.Data._sql(strValue);
        }

        public void calculatePenalty()
        {
            double decPenaltyRate = double.Parse(this.getRate());
            DateTime dtDueDate = Utils.Unix.DateTimeFromUnixTimestampSeconds(long.Parse(this.getDate()));
            dtDueDate = dtDueDate.AddMonths(2);
            dtDueDate = dtDueDate.AddDays(-1);
            //MessageBox.Show(dtDueDate.ToString("dd/MM/yyyy"));
            this.setDueDate(Utils.Unix.GetUnixTimestampSecondsFromDateTime(dtDueDate).ToString());

            this.setPercent(Properties.Settings.Default.PENALTY_PERCENT_VALUE.ToString());
            
            int intDaysCount = ((int)((DateTime.Now - dtDueDate).TotalDays));
            if(intDaysCount > Properties.Settings.Default.PENALTY_DAY_COUNT_LIMIT) intDaysCount = Properties.Settings.Default.PENALTY_DAY_COUNT_LIMIT;
            if (intDaysCount < 0)
            {
                this.setPercent("0");
                this.setDaysCount("0");
            }
            else
            {
                this.setDaysCount(intDaysCount.ToString());
            }
            

            double dbPenalityPerDay = (double)(Properties.Settings.Default.PENALTY_PERCENT_VALUE / 100 * int.Parse(this.getDaysCount()));
            this.setPenaltyPerDay(dbPenalityPerDay.ToString());

            //MessageBox.Show(objPenalty2Insert.getDaysCount());
            //MessageBox.Show(objPenalty2Insert.getPenaltyPerDay());
            double dbPenaltyValue = Math.Round((decPenaltyRate * dbPenalityPerDay), 2, MidpointRounding.AwayFromZero);
            this.setPenaltyValue(dbPenaltyValue.ToString());

            this.setTotalPenalty(Math.Round((dbPenaltyValue + decPenaltyRate), 2, MidpointRounding.AwayFromZero).ToString());

        }

    }
}
